package com.bw.produce.service.impl;

import com.bw.common.core.domain.R;
import com.bw.common.core.exception.produce.RepeatException;
import com.bw.produce.domain.Brand;
import com.bw.produce.mapper.BrandMapper;
import com.bw.produce.query.BrandQueryObject;
import com.bw.produce.service.IBrandService;
import com.bw.produce.api.RemoteFileService;
import com.bw.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author lemon
 * @date 2024/11/12 21:36
 **/
@Service
public class BrandServiceImpl implements IBrandService {

    @Autowired
    BrandMapper brandMapper;


    @Autowired
    RemoteFileService remoteFileService;

    @Override
    public List<Brand> selectBrandList(BrandQueryObject queryParams) {
        return brandMapper.selectAll(queryParams);
    }

    @Override
    public void insertBrand(Brand brand) {
        if(brandMapper.selectByBrandName(brand.getBrandName()) > 0){
            throw new RepeatException("该商品名已存在");
        }
        brandMapper.insert(brand);
    }

    @Override
    public void updateBrand(Brand brand) {
        if (brand.getId() == null){
            throw new RepeatException("id不能为空");
        }
        brandMapper.update(brand);
    }


    @Override
    public void deleteBrandById(Long[] brandId) {
        for (Long id : brandId) {
            if(brandMapper.selectById(id) < 0){
                throw new RepeatException("删除的品牌不存在");
            }
            //删除相关的中间表数据 TODO
        }
        brandMapper.deleteByIds(brandId);
    }

    @Override
    public R<SysFile> upload(MultipartFile file) {
        return remoteFileService.upload(file);
    }

    @Override
    public Brand getInfo(Long brandId) {
        return brandMapper.selectInfo(brandId);
    }

    @Override
    public void changeManufacturer(Long brandId) {
        brandMapper.updateManufacturer(brandId);
    }

    @Override
    public void changeReveal(Long brandId) {
        brandMapper.updateReveal(brandId);
    }


}
